package ClientFront;

import java.io.IOException;
import java.net.Socket;
import java.util.regex.Pattern;

public class MyUtils {
    public static boolean isValidIPv4Address(String address){
        //{0~255.}*3+{0~255}
        //({0~9}|{1~9}+{0~9}|1+{0~9}*2|2+{0~4}+{0~9}|2+5+{0~5})+.
        //^    *3 + ~
        //=^{{{0~9}|{1~9}+{0~9}|1+{0~9}*2|2+{0~4}+{0~9}|2+5+{0~5})+.}*3}+
        //  {{0~9}|{1~9}+{0~9}|1+{0~9}*2|2+{0~4}+{0~9}|2+5+{0~5}}$
        String ipPattern = "^(((25[0-5])|(2[0-4]\\d)|(1\\d{2})|([1-9]?\\d))\\.){3}((25[0-5])|(2[0-4]\\d)|(1\\d{2})|([1-9]?\\d))$";
        return Pattern.matches(ipPattern,address);
    }
    public static boolean isValidUserName(String name){
        //以数字或英文字母开头，长度大于1
        String namePattern = "([0-9]|[a-z]|[A-Z])([0-9]|[a-z]|[A-Z])*";
        return Pattern.matches(namePattern,name);
    }
    public static boolean isValidPort(String port){
        String portPattern = "^([0-9])([0-9])*";
        if(Pattern.matches(portPattern,port)){
            return isValidPort(Integer.parseInt(port));
        }
        return false;
    }
    public static boolean isValidPort(int port){
        if(port <= 0 || port >= 65535){
            return false;
        }
            return true;
    }
    public static int intToStep(int xAxis,int yAxis)
    {
        if(xAxis > 15 || yAxis > 15 || xAxis < 0 || yAxis < 0){
            return 0;
        }
        int ret = xAxis;
        ret <<= 4;
        ret += yAxis;
        return ret;
    }
    public static int[] stepToint(int step){
        int[] ret = new int[2];
        ret[1] = step & 15;//step & 00001111
        ret[0] = step >> 4;
        return ret;
    }
}
